﻿
@page "/User/Login"
@using HandlerContextOptions
@using UserCenter.Interfaces
@layout EmptyLayout
@inject IUserBasics iUserBasics



<div class="login-NavLogin">
    <div style="padding:30px">
        <Card Bordered="true" Title=@("用户登录") Style="width:400px" Hoverable="true">
            <Body>
                <Form Model="@request" OnFinish="HandleSubmit">
                    <FormItem Label="用户">
                        <AntDesign.Input Placeholder="请填写用户名" Size="large" @bind-Value="@context.Name">
                            <Prefix><Icon Type="user" /></Prefix>
                        </AntDesign.Input>
                    </FormItem>
                    <FormItem Label="密码">
                        <AntDesign.Input Placeholder="请填写用户密码" Size="large" @bind-Value="@context.Pwd" Type="password">
                            <Prefix><Icon Type="lock" /></Prefix>
                        </AntDesign.Input>
                    </FormItem>
                    <Space Direction="DirectionVHType.Vertical" Style="width:100%">
                        <SpaceItem>
                            <Spin Spinning="Loading" Size="large" Tip="验证中...">
                                <Button Type="primary" HtmlType="submit" Class="submit" Size="large" Block>提&nbsp;交</Button>
                            </Spin>
                        </SpaceItem>
                    </Space>
                </Form>
            </Body>
        </Card>
    </div>
</div>

@code {

    WindowsHelper windows;

    bool Loading { get; set; }

    UserLoginRequset request = new UserLoginRequset();

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        windows = new WindowsHelper(jSRuntime);
        var user = await windows.GetLocalStorageAsync<UserLoginResult>();
        if (user != null)
        {
            navigationManager.NavigateTo("/");
        }
        await windows.HideLoadingAsync();
        await windows.SetTitleAsync("Login");
    }

    protected async Task HandleSubmit()
    {
        Loading = true;

        var result = await iUserBasics.LoginAsync(request);

        Loading = false;

        await windows.ConsoleLogAsync(result);

        this.Notification(result);

    }

    private async void Notification(UserLoginResult result)
    {
        if (result.IsSuccessful)
        {
            await windows.SetLocalStorageAsync(result);
            navigationManager.NavigateTo("/");
        }
        else
        {
            await notice.Error(new NotificationConfig
            {
                Message = "登录失败",
                Description = result.Description
            });
        }
    }

}

<style>
    .login-NavLogin {
        position: absolute;
        left: 50%;
        top: 40%;
        transform: translate(-50%, -50%);
    }
</style>